﻿
CREATE FUNCTION [dbo].[TrackerRequest_PendingRemove](
	@TrackerRequestID int,
	@VIN varchar(17)
)RETURNS int AS BEGIN

RETURN(
	SELECT  TOP 1 tr.ID	FROM t_TrackerRequest tr
	INNER JOIN v_Menu_10 m ON tr.StatusID = m.MenuID
	INNER JOIN t_Tracker_Car tc ON tr.VIN = tc.VIN
	WHERE tr.VIN = @VIN AND
				tr.ID <> @TrackerRequestID AND
				m.WorkFlowIndex BETWEEN 615 AND 625
	ORDER BY m.WorkFlowIndex DESC
)
END

